﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public abstract class CinemaCurveTrackControl : TimelineTrackControl
{
    private int controlSelection;
    private CinemaCurveClipItemControl focusedControl;
    protected int MINIMUM_ROWS_SHOWING = 3;
    protected int rowsShowing = 5;

    protected CinemaCurveTrackControl()
    {
    }

    public override void calculateHeight()
    {
        if (this.IsExpanded)
        {
            this.trackArea.height = 17f * this.rowsShowing;
        }
        else
        {
            this.trackArea.height = 17f;
        }
    }

    private void editItemControl(int controlSelection)
    {
        int num = 0;
        foreach (CinemaCurveClipItemControl control in base.Controls)
        {
            control.IsEditing = num++ == controlSelection;
            if (control.IsEditing)
            {
                this.focusedControl = control;
                this.MINIMUM_ROWS_SHOWING = 10;
                this.rowsShowing = 10;
            }
        }
    }

    private void updateEditPanel(Rect row1)
    {
        Rect position = new Rect(row1.x, row1.y, row1.width / 2f, row1.height);
        Rect rect2 = new Rect(row1.x + (row1.width / 2f), row1.y, row1.width / 2f, row1.height);
        List<GUIContent> list = new List<GUIContent>();
        foreach (TrackItemControl control in base.Controls)
        {
            if ((control.Wrapper != null) && (control.Wrapper.Behaviour != null))
            {
                list.Add(new GUIContent(control.Wrapper.Behaviour.name));
            }
        }
        if (list.Count > 0)
        {
            this.controlSelection = Mathf.Min(this.controlSelection, list.Count - 1);
            this.controlSelection = EditorGUI.Popup(position, this.controlSelection, list.ToArray());
            if (GUI.Button(rect2, new GUIContent("Edit")))
            {
                this.editItemControl(this.controlSelection);
            }
        }
    }

    public override void UpdateHeaderContents(DirectorControlState state, Rect position, Rect headerBackground)
    {
        Rect rect = new Rect(position.x + 18f, position.y, (position.width - 18f) - 64f, 17f);
        this.IsExpanded = EditorGUI.Foldout(rect, this.IsExpanded, base.TargetTrack.Behaviour.name, true);
        int controlID = GUIUtility.GetControlID(base.TargetTrack.Behaviour.GetInstanceID(), FocusType.Passive, rect);
        if (((Event.current.GetTypeForControl(controlID) == EventType.MouseDown) && rect.Contains(Event.current.mousePosition)) && (Event.current.button == 1))
        {
            base.showHeaderContextMenu();
            Event.current.Use();
        }
        this.updateHeaderControl1(new Rect(position.width - 64f, position.y, 16f, 16f));
        this.updateHeaderControl2(new Rect(position.width - 48f, position.y, 16f, 16f));
        this.updateHeaderControl3(new Rect(position.width - 32f, position.y, 16f, 16f));
        this.updateHeaderControl4(new Rect(position.width - 16f, position.y, 16f, 16f));
        if (this.IsExpanded)
        {
            Rect rect2 = new Rect(rect.x + 36f, position.y + 17f, (headerBackground.width - rect.x) - 36f, 17f);
            if (((this.focusedControl != null) && (this.focusedControl.Wrapper != null)) && (this.focusedControl.Wrapper.Behaviour != null))
            {
                new Rect(rect2.x, rect2.y, rect2.width / 2f, rect2.height);
                Rect rect3 = new Rect(rect2.x + (rect2.width / 2f), rect2.y, rect2.width / 2f, rect2.height);
                Rect controlHeaderArea = new Rect(rect2.x, rect2.y + rect2.height, rect2.width, position.height - 34f);
                this.focusedControl.UpdateHeaderArea(state, controlHeaderArea);
                if (GUI.Button(rect3, new GUIContent("Done")))
                {
                    this.focusedControl = null;
                    this.editItemControl(-1);
                    this.MINIMUM_ROWS_SHOWING = 3;
                    this.rowsShowing = 5;
                }
            }
            else
            {
                this.updateEditPanel(rect2);
            }
        }
    }

    protected override void updateHeaderControl1(Rect position)
    {
        if (GUI.Button(position, string.Empty, TimelineTrackControl.styles.compressStyle) && (this.rowsShowing > this.MINIMUM_ROWS_SHOWING))
        {
            this.rowsShowing--;
        }
    }

    protected override void updateHeaderControl2(Rect position)
    {
        if (GUI.Button(position, string.Empty, TimelineTrackControl.styles.expandStyle))
        {
            this.rowsShowing++;
        }
    }

    public bool IsExpanded
    {
        get
        {
            string key = string.Format("{0}.{1}", base.TargetTrack.Behaviour.GetInstanceID(), "isExpanded");
            return (EditorPrefs.HasKey(key) && EditorPrefs.GetBool(key));
        }
        set
        {
            EditorPrefs.SetBool(string.Format("{0}.{1}", base.TargetTrack.Behaviour.GetInstanceID(), "isExpanded"), value);
        }
    }
}

